﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class SubscriptionBL
    {
        public List<Subscription> GetAll()
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<Subscription>(Common.Constants.StoredProcedures.GetAllSubscriptions, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        public int CreateSubscription(Subscription subscript)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@Code", subscript.Code);
                adapter.AddParameter("@Title", subscript.Title);
                adapter.AddParameter("@Status", subscript.Status);
                adapter.AddParameter("@NumberOfDays", subscript.NumberOfDays);
                adapter.AddParameter("@Price", subscript.Price);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertSubscription, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateSubscription(Subscription subscript)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@SubscriptionID", subscript.SubscriptionID);
                adapter.AddParameter("@Code", subscript.Code);
                adapter.AddParameter("@Title", subscript.Title);
                adapter.AddParameter("@Status", subscript.Status);
                adapter.AddParameter("@NumberOfDays", subscript.NumberOfDays);
                adapter.AddParameter("@Price", subscript.Price);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateSubscription, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public Subscription GetByCode(string code)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Code", code);
            var list = adapter.GetList<Subscription>(Common.Constants.StoredProcedures.GetSubscriptionByCode, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public Subscription GetBySubscriptionID(int id)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", id);
            var list = adapter.GetList<Subscription>(Common.Constants.StoredProcedures.GetSubscriptionByID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public int DeleteSubscriptionByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                    result = adapter.ExecuteNonQuery(String.Format("DELETE FROM Subscriptions WHERE SubscriptionID IN ({0});", ids), CommandType.Text, CustomConnectionState.CloseOnExit);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
    }
}
